﻿using System.Collections.Generic;
using System.Linq;
using Journal.Domain.IDomainServices;
using Journal.Domain.Models;
using Journal.Domain.RepositoryInterfaces;

namespace Journal.Domain.DomainServices
{
    public class StudyGroupDomainService : BaseDomainService<StudyGroup>, IStudyGroupDomainService
    {
        public StudyGroupDomainService(IBaseRepository<StudyGroup> repository) : base(repository)
        {
        }

        public IList<string> GetStudyGroupsNameByUserId(int userId)
        {
            return Repository.Query().Where(p => p.User.Id == userId).Select(p => p.Name).ToList();
        }

        public IList<string> GetAllStudyGroupName(int educationInstitutionId)
        {
            return Repository.Query().Where(p => p.EducationInstitution.Id == educationInstitutionId).Select(p => p.Name).ToList();
        }

        public IList<StudyGroup> GetAllStudyGroupByEducationInstitutionId(int educationInstitutionId)
        {
            return Repository.Query().Where(p => p.EducationInstitution.Id == educationInstitutionId).ToList();
        }

        public IList<StudyGroup> GetStudyGroupsByUserId(int userId)
        {
            return Repository.Query().Where(p => p.User.Id == userId).ToList();
        }

        public void DeleteStudent(StudyGroup group, int studentId)
        {
            User user = group.Students.FirstOrDefault(x => x.Id == studentId);
            if(user != null)
            {
                group.Students.Remove(user);
            }

            Repository.Save(group);
        }
    }
}